<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit" :showHistoryBtn="true">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<om-white-space bgColor="#eee"></om-white-space>
	</om-addpage>
</template>

<script>
import { debounce,getUrl,confirmModal, ptFloat, minusF } from '@/static/js/common.js';
import {buildFormData, formValidation, tranferToEditors, changeEditor, configSelectData, hasValue,formShowLoading ,getFormFields,formModelLoad, getEditorTitle} from '@/static/js/formUtil.js';
import urlUtil from '@/static/js/urlUtil.js';
import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return buildFormData({
			currencyCacheKey:'fin-pay-loans-currency-cache',
			initValue: {
				zuserId:"",
				zuserName:"",
				loanMoney:"",
				currencyNameId:'',
				currencyName:''
			}
		})
	},
	computed: {
		...mapState(useLoginUserStore, ['userName', 'userId'])
	},
	watch:{
		'formdata.loanMoney':{//借款金额
			 handler(){
				 this.$nextTick(function(){
				 	this.getCapitalPaySum()
				 })
			 }
		 }
		
	},
	methods: {
		getCapitalPaySum: debounce(function(){
				let _that = this;
				if(_that.formdata.loanMoney){
					_that.om.request("common/getCapitalNum", {
						moneyNum: _that.formdata.loanMoney
					}).then(res => {
						if (res.succeed) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,'loanMoneyCapital',res.value)
								_that.$set(_that.formdata,'loanMoneyCapital',res.value)
						}
					})
				}else{
					_that.$refs.autoform.formdata.loanMoneyCapital = ""
				}
			},800),
		
		formSubmit() {
			let _that = this;
			_that.om.formSave('wxLoan/save', _that.formdata, _that)
		},
		onSelectPayType(item) {
			let _that = this;
			if(item.code == "37848"){
				_that.om.request(
					'temployee/getBankInfo',
					{
						userId:_that.userId
					}
				).then(res=>{
					_that.$refs.autoform.formdata.accountName = res.value.username
					_that.$refs.autoform.formdata.card = res.value.salaryCard
				})
				_that.$refs.autoform.changeEditor("titleInfo","bank","type",'textarea')
				_that.$refs.autoform.changeEditor("titleInfo","card","type",'textarea')
				_that.$refs.autoform.changeEditor("titleInfo","bank","required",true)
				_that.$refs.autoform.changeEditor("titleInfo","card","required",true)
			}else{
				_that.$refs.autoform.clear("titleInfo","accountName")
				_that.$refs.autoform.clear("titleInfo","bank")
				_that.$refs.autoform.clear("titleInfo","card")
				_that.$refs.autoform.changeEditor("titleInfo","bank","type",'labelbox')
				_that.$refs.autoform.changeEditor("titleInfo","card","type",'labelbox')
				_that.$refs.autoform.changeEditor("titleInfo","bank","required",false)
				_that.$refs.autoform.changeEditor("titleInfo","card","required",false)
			}
		},
		clearPayType(){
			let _that = this;
			_that.$refs.autoform.clear("titleInfo","payTypeCode")
			_that.$refs.autoform.clear("titleInfo","accountName")
			_that.$refs.autoform.clear("titleInfo","bank")
			_that.$refs.autoform.clear("titleInfo","card")
			_that.$refs.autoform.changeEditor("titleInfo","bank","required",false)
			_that.$refs.autoform.changeEditor("titleInfo","card","required",false)
		},
		initLoad(options) {
			let _that = this;
			var fileId = options.fileId
			var ajaxArr = [
				urlUtil.sysdictComboboxUrl("38044"),//借款类别
				{
					url:'dictZero/getListByCode',
					param:{
						code:'37846',
						inCode:'37847,37848'
					}
				},//付款方式
				{url:"tloan/setValueInfo"},//已借款信息
				{
					url:'currencyDict/getLocalCurrency'
				}
			];
			if(fileId){
				ajaxArr.push({
					url:'tloan/getInfo',
					param:{
						id:fileId
					},
					method:'post'
				})
			}
			formModelLoad(ajaxArr, function(res, mainData) {
				changeEditor("titleInfo", 'loanTypeId', 'data', configSelectData(res[0], 'id', 'dicname'), mainData);
				changeEditor("titleInfo", 'payTypeCode', 'data', configSelectData(res[1], 'code', 'dicname'), mainData);
				if(res[2].succeed && !_that.edit){
					changeEditor("titleInfo", 'alreadyLoanMoneyStr', 'defaultValue', res[2].value.moneyStr, mainData);
					changeEditor("titleInfo", 'firstDateStr', 'defaultValue', res[2].value.firstDateStr, mainData);
				}
				if(res[3].succeed && res[3].value && !_that.edit){
					_that.initValue.currencyNameId = res[3].value.id
					_that.initValue.currencyName = res[3].value.codeNameUnit
					changeEditor("titleInfo","loanMoney","suffix",res[3].value.currencyUnit,mainData)
				}
				changeEditor("titleInfo","currencyNameId",{
					link:'../../public/pageSearch/pageSearch',
					'linkParam':{
						other:JSON.stringify({}),
						url:'currencyDict/getCurrency',
						namefield:'currencyCode',
						notefield:'currencyName',
						initload:true,
						cachename:_that.currencyCacheKey
					}
				},null,mainData)
				
				if(fileId){
					let fieldArr = getFormFields(mainData)
					fieldArr.push('id')
					for(let index in res[4].value.tloan){
						if(fieldArr.includes(index)){
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[4].value.tloan[index])
							_that.$set(_that.formdata,index,res[4].value.tloan[index])
						}
					}
					if(_that.$refs.autoform.formdata.payTypeCode == "37848"){
						changeEditor("titleInfo","bank","type",'textarea',mainData)
						changeEditor("titleInfo","card","type",'textarea',mainData)
						changeEditor("titleInfo","bank","required",true,mainData)
						changeEditor("titleInfo","card","required",true,mainData)
					}else{
						changeEditor("titleInfo","bank","type",'labelbox',mainData)
						changeEditor("titleInfo","card","type",'labelbox',mainData)
						changeEditor("titleInfo","bank","required",false,mainData)
						changeEditor("titleInfo","card","required",false,mainData)
					}
					_that.$nextTick(function(){
						_that.$refs.autoform.initSelectIndex('titleInfo','loanTypeId')
						_that.$refs.autoform.initSelectIndex('titleInfo','payTypeCode')
					})
				}
			},options)
		},
	},
	onUnload(){
		//#ifdef APP-PLUS
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				uni.removeStorage({
					key: getWorkCacheKey(fieldId)
				});
			}
		}
		//#endif
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			for (var fieldId in _that.subEditorObj) {
				let editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
			uni.getStorage({
				key:_that.currencyCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.currencyNameId = d.id
						formdata.currencyName = d.codeNameUnit
						_that.$refs.autoform.changeEditor("titleInfo","loanMoney","suffix",d.currencyUnit)
						uni.removeStorage({
							key:_that.currencyCacheKey
						})
					}
				}
			})
		}
	},
	onLoad(options) {
		let _that = this;
		_that.initValue.zuserId = this.userId;
		_that.initValue.zuserName = this.userName;
		_that.initLoad(options);
	}
};
</script>
<style></style>
